/**

 @Name：主页展示设备
 @Author：GROW
    
 */


layui.define(['table', 'form'], function(exports) {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        table = layui.table,
        form = layui.form,

        //事件
        active = {
            invalid: function() {
                var checkStatus = table.checkStatus('LAY-equipment-back-manage'),
                    checkData = checkStatus.data, //得到选中的数据
                    ids = [];

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                for (var i = 0; i < checkData.length; i++) {
                    ids.push(checkData[i]['id'])
                }

                layer.confirm('确定使失效吗？', function(index) {

                    admin.req({
                        type: 'post',
                        url: '/equipment/invalidEquipment',
                        data: { 'invalidData': JSON.stringify(ids) },
                        success: function(res) {
                            if (res.code == 0) {
                                table.reload('LAY-equipment-back-manage');
                                layer.msg('已失效');
                            } else {
                                table.reload('LAY-equipment-back-manage');
                                layer.msg("失效设备失败");
                            }
                        }
                    });

                    table.reload('LAY-equipment-back-manage');
                    layer.close(index);
                });
            },

            add: function() {
                admin.popup({
                    title: '添加新设备',
                    area: ['420px', '650px'],
                    id: 'LAY-popup-equipment-add',
                    success: function(layero, index) {
                        view(this.id).render('equipment/equipmentform').done(function() {

                            form.render(null, 'layuiadmin-form-equipment');

                            //监听提交
                            form.on('submit(LAY-user-back-submit)', function(data) {
                                var field = data.field, //获取提交的字段
                                    tableData = table.cache["LAY-details-back-manage"];

                                for (var i = 0; i < tableData.length; i++) {
                                    delete tableData[i].LAY_TABLE_INDEX;
                                }

                                field['details'] = JSON.stringify(tableData);

                                admin.req({
                                    type: 'post',
                                    url: '/equipment/addEquipment',
                                    data: field,
                                    success: function(res) {
                                        if (res.code == 0) {
                                            layer.msg("新增设备成功");
                                        } else {
                                            return layer.msg("新增设备失败");
                                        }
                                    }
                                });

                                layui.table.reload('LAY-equipment-back-manage'); //重载表格
                                layer.close(index); //执行关闭 
                            });
                        });
                    }
                });
            }
        };


    //管理员管理
    table.render({
        elem: '#LAY-equipment-back-manage',
        url: './equipment/getEquipments',
        cellMinWidth: 80,
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                { field: 'id', title: 'ID', sort: true },
                { field: 'name', title: '名称' },
                { field: 'producer', title: '生产商' },
                {
                    field: 'type',
                    title: '类型',
                    templet: function(item) {            
                        if (item.type == "1") return "PC";              
                        else if (item.type == "2") return "显示器";
                        else if (item.type == "3") return "电源";              
                        else return "";              
                    }
                },
                { field: 'creator', title: '创建者' },
                { field: 'createDate', title: '创建时间', sort: true },
                { title: '操作', align: 'center', fixed: 'right', toolbar: '#table-equipment-admin' }
            ]
        ],
        text: { none: '暂无相关数据' },
        page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['prev', 'page', 'next', 'skip', 'count'], //自定义分页布局
            groups: 3 //只显示 1 个连续页码
        },
        limit: 10,
        where: { 'effective': 1 }
    });

    //监听工具条
    table.on('tool(LAY-equipment-back-manage)', function(obj) {
        var data = obj.data;
        if (obj.event === 'invalid') {
            var ids = [];

            ids.push(data['id'])

            layer.confirm('确定失效吗？', function(index) {

                admin.req({
                    type: 'post',
                    url: '/equipment/invalidEquipment',
                    data: { 'invalidData': JSON.stringify(ids) },
                    success: function(res) {
                        if (res.code == 0) {
                            table.reload('LAY-equipment-back-manage');
                            layer.msg('已失效');
                        } else {
                            table.reload('LAY-equipment-back-manage');
                            layer.msg("失效设备失败");
                        }
                    }
                });

                table.reload('LAY-equipment-back-manage');
                layer.close(index);
            });
        } else if (obj.event === 'modify') {
            admin.popup({
                title: '修改设备信息',
                area: ['420px', '650px'],
                id: 'LAY-popup-equipment-add',
                success: function(layero, index) {
                    view(this.id).render('equipment/equipmentform', data).done(function() {

                        table.reload('LAY-details-back-manage', {
                            data: data.details
                        });

                        form.render(null, 'layuiadmin-form-equipment');

                        //监听提交
                        form.on('submit(LAY-user-back-submit)', function(info) {
                            var field = info.field, //获取提交的字段
                                tableData = table.cache["LAY-details-back-manage"];

                            for (var i = 0; i < tableData.length; i++) {
                                delete tableData[i].LAY_TABLE_INDEX;
                            }

                            field['id'] = data.id;
                            field['details'] = JSON.stringify(tableData);

                            admin.req({
                                type: 'post',
                                url: '/equipment/updateEquipment',
                                data: field,
                                success: function(res) {
                                    if (res.code == 0) {
                                        layer.msg("修改设备成功");
                                    } else {
                                        return layer.msg("修改设备失败");
                                    }
                                }
                            });

                            layui.table.reload('LAY-equipment-back-manage'); //重载表格
                            layer.close(index); //执行关闭 
                        });
                    });
                }
            })
        }
    });

    form.render(null, 'layadmin-equipment-formlist');

    //监听搜索
    form.on('submit(LAY-equipment-back-search)', function(data) {
        var field = data.field;
        field['effective'] = 1;
        //执行重载
        table.reload('LAY-equipment-back-manage', {
            where: field
        });
    });

    $('.layui-btn.layuiadmin-btn-admin').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports('adminequipment', {})
});